/*
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2023-03-29 10:48:24
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2023-03-31 09:39:45
 * @FilePath: \zs-grouting2\naive-ui-admin\src\modules\system\department\department.service.ts
 * @Description:部门管理
 */
import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { Application, ApplicationController } from './department.controller'

export const tableState = useTable<Application>()
export const formState = useForm<Application>()

export const init = () => {
  // tableState.columns = [{ type: 'selection' }, { title: '部门名称', key: 'applicationLogo', slot: true }]
  formState.template = [
    {
      type: 'select',
      label: '上级部门名称:',
      keyPath: 'applicationName',
      selectProps: {
        options: [],
        placeholder: '请选择上级部门名称,默认无上级部门'
      }
    },
    {
      type: 'text',
      label: '部门名称:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入上级部门名称' }
    }
  ]
}

@Injectable({
  controllers: [ApplicationController]
})
export class ApplicationService {
  static applicationController: ApplicationController

  static async getDepartmentList() {
    console.log(formState.template![0])
    formState.template![0].selectProps!.options = [
      { label: '开发部', value: 1 },
      { label: '管理部', value: 2 },
      { label: '检测部', value: 3 },
      { label: '业主部', value: 4 }
    ]
  }

  static async getApplicationList({ page, size }: PaginationDto) {
    const res = await this.applicationController.findListReq({ page, size })!
    if (res.code === 200) {
      res.data.list.forEach((item) => {
        item.applicationLogo = item.applicationLogo
          ? import.meta.env.BUTTERFLY_BASE_API_URL + item.applicationLogo
          : ''
      })
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return { list: [{ applicationLogo: '开发部' }, { applicationLogo: '监测部' }], total: 0 }
  }

  static async createOne(newData: Application) {
    const res = await this.applicationController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Application) {
    const res = await this.applicationController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.applicationController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size) {
    return ApplicationService.getApplicationList({ page, size })
  },
  c(newData) {
    return ApplicationService.createOne(newData)
  },
  u(newData) {
    return ApplicationService.updateOne(newData)
  },
  d(keys) {
    return ApplicationService.deleteByIds(keys)
  }
})
